#include "ssSysComm.h"
#include "commonDefine.h"

int ss_vpss_chn_cache(int grp,int chn,int depth){
    ot_vpss_chn_attr vpss_chn_attr = {};
    td_s32 s32Ret = ss_mpi_vpss_get_chn_attr(grp,chn,&vpss_chn_attr);
    if(s32Ret != TD_SUCCESS){
        ERRO_PRT("error ss_mpi_vpss_get_chn_attr %x,grp %d,chn %d\n",s32Ret,grp,chn);
        return -1;
    }

    vpss_chn_attr.depth = depth;
    //vpss_chn_attr.width = 1920;
    //vpss_chn_attr.height = 1080;
    s32Ret = ss_mpi_vpss_set_chn_attr(grp,chn,&vpss_chn_attr);
    if(s32Ret != TD_SUCCESS){
        ERRO_PRT("error ss_mpi_vpss_set_chn_attr %x,grp %d,chn %d\n",s32Ret,grp,chn);
        return -1;
    }
    return 0;
}

int ss_vpss_chn_get_frame(int grp,int chn,ot_video_frame_info* frame,int timeoutms){
    td_s32 s32Ret = ss_mpi_vpss_get_chn_frame(grp, chn, frame, timeoutms);
    if (s32Ret != TD_SUCCESS)
    {
        ERRO_PRT("error ss_mpi_vpss_get_chn_frame %x\n", s32Ret);    
        return -1;
    }

    uint32_t image_size = 0;
    switch (frame->video_frame.pixel_format)
    {
    case OT_PIXEL_FORMAT_YVU_SEMIPLANAR_420:
        image_size = frame->video_frame.height * frame->video_frame.stride[0] *1.5;
        
        break;
    case OT_PIXEL_FORMAT_YUV_400:
        image_size = frame->video_frame.height * frame->video_frame.stride[0];
        break;
    
    default:
        ERRO_PRT("error not implament format\n");
        return -1;
    }

    
    frame->video_frame.virt_addr[0] = ss_mpi_sys_mmap_cached(frame->video_frame.phys_addr[0],image_size);
    if(frame->video_frame.virt_addr[0] == 0){
        ERRO_PRT("error ss_mpi_sys_mmap_cached,size %d\n",image_size);
        return -1;
    }

    return 0;
}


int ss_vpss_chn_release_frame(int grp,int chn,ot_video_frame_info* frame){

    uint32_t image_size = 0;
    switch (frame->video_frame.pixel_format)
    {
    case OT_PIXEL_FORMAT_YVU_SEMIPLANAR_420:
        image_size = frame->video_frame.height * frame->video_frame.stride[0] *1.5;
        break;
        
    case OT_PIXEL_FORMAT_YUV_400:
        image_size = frame->video_frame.height * frame->video_frame.stride[0];
        break;
    default:
        ERRO_PRT("error not implament format\n");
        return -1;
    }

    td_s32 s32Ret = ss_mpi_sys_munmap(frame->video_frame.virt_addr[0],image_size);
    if(s32Ret != TD_SUCCESS){
        ERRO_PRT("ss_mpi_sys_munmap %x\n",s32Ret);
        return -1;
    }

    s32Ret = ss_mpi_vpss_release_chn_frame(grp,chn,frame);
    if(s32Ret != TD_SUCCESS){
        ERRO_PRT("ss_mpi_vpss_release_chn_frame %x\n",s32Ret);
        return -1;
    }
    
    return 0;
}